package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BlnoRuleYearErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.BlnoRuleYearDO;
import com.cmc.cloud.cmclink.doc.mapper.BlnoRuleYearMapper;
import com.cmc.cloud.cmclink.doc.service.BlnoRuleYearService;
import com.cmc.cloud.cmclink.doc.vo.blnoruleyearvo.BlnoRuleYearBaseVO;
import com.cmc.cloud.cmclink.doc.vo.blnoruleyearvo.BlnoRuleYearCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnoruleyearvo.BlnoRuleYearPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnoruleyearvo.BlnoRuleYearUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;


/**
 * 订舱号提单号规则 年份 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class BlnoRuleYearServiceImpl implements BlnoRuleYearService {

    @Resource
    private BlnoRuleYearMapper blnoRuleYearMapper;

    @Override
    @Lock4j(name = "blnoRuleYearUnique", keys = {"#createReqVO.getYearBlnoCode", "#createReqVO.getEtdYear"})
    public void createBlnoRuleYear(BlnoRuleYearCreateReqVO createReqVO) {
        // 插入
        validateCodeExists(createReqVO, null);
        BlnoRuleYearDO blnoRuleYear = BeanUtil.copyProperties(createReqVO, BlnoRuleYearDO.class);
        blnoRuleYearMapper.insert(blnoRuleYear);
    }

    @Override
    @Lock4j(name = "blnoRuleYearUnique", keys = {"#updateReqVO.getYearBlnoCode", "#updateReqVO.getEtdYear"})
    public void updateBlnoRuleYear(BlnoRuleYearUpdateReqVO updateReqVO) {
        // 校验存在
        validateBlnoRuleYearExists(updateReqVO.getId());
        validateCodeExists(updateReqVO, updateReqVO.getId());
        // 更新
        BlnoRuleYearDO updateObj = BeanUtil.copyProperties(updateReqVO, BlnoRuleYearDO.class);
        blnoRuleYearMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlnoRuleYear(Long id) {
        // 校验存在
        validateBlnoRuleYearExists(id);
        // 删除
        blnoRuleYearMapper.deleteById(id);
    }

    private void validateBlnoRuleYearExists(Long id) {
        if (blnoRuleYearMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(BlnoRuleYearErrorCodeConstants.BLNO_RULE_YEAR_NOT_EXISTS);
        }
    }

    private void validateCodeExists(BlnoRuleYearBaseVO vo, Long id) {
        LambdaQueryWrapperX<BlnoRuleYearDO> queryWrapperX = new LambdaQueryWrapperX<BlnoRuleYearDO>()
                .eqIfPresent(BlnoRuleYearDO::getYearBlnoCode, vo.getYearBlnoCode());
        if (id != null) {
            queryWrapperX.ne(BlnoRuleYearDO::getId, id);
        }
        if (blnoRuleYearMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(BlnoRuleYearErrorCodeConstants.BLNO_RULE_YEAR_CODE_EXISTED);
        }
        LambdaQueryWrapperX<BlnoRuleYearDO> queryWrapperX1 = new LambdaQueryWrapperX<BlnoRuleYearDO>()
                .eqIfPresent(BlnoRuleYearDO::getEtdYear, vo.getEtdYear());
        if (id != null) {
            queryWrapperX1.ne(BlnoRuleYearDO::getId, id);
        }
        if (blnoRuleYearMapper.selectCount(queryWrapperX1) != 0L) {
            throw ServiceExceptionUtil.exception(BlnoRuleYearErrorCodeConstants.BLNO_RULE_YEAR_EXISTED);
        }
    }

    @Override
    public BlnoRuleYearDO getBlnoRuleYear(Long id) {
        return blnoRuleYearMapper.selectById(id);
    }

    @Override
    public List<BlnoRuleYearDO> getBlnoRuleYearList() {
        List<BlnoRuleYearDO> blnoRuleYearDos = blnoRuleYearMapper.selectList();
        return blnoRuleYearDos;
    }

    @Override
    public PageResult<BlnoRuleYearDO> getBlnoRuleYearPage(BlnoRuleYearPageReqVO reqVO) {
        LambdaQueryWrapperX<BlnoRuleYearDO> queryWrapperX = new LambdaQueryWrapperX<>();
        return blnoRuleYearMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public BlnoRuleYearDO selectBlnoRuleYearOne(String etdYear) {
        LambdaQueryWrapperX<BlnoRuleYearDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.in(BlnoRuleYearDO::getEtdYear, etdYear);
        queryWrapperX.eq(BlnoRuleYearDO::getEnable, true);
        return blnoRuleYearMapper.selectOne(queryWrapperX);
    }
}
